package com.chenguo.learn.redis.service.impl;

import com.chenguo.learn.redis.config.RedisConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Service
public class RedisPoolFactory {
    private static Logger logger = LoggerFactory.getLogger(RedisPoolFactory.class);
    @Autowired
    RedisConfig redisConfig;

    @Bean
    public JedisPool JedisPoolFactory() {
//        logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>host:" +redisConfig.getHost());
//        logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>port:" +redisConfig.getPort());
//        logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>poolMaxIdle:" +redisConfig.getPoolMaxIdle());
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());
        poolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
        poolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait() * 1000);
        JedisPool jp = new JedisPool(poolConfig, redisConfig.getHost(), redisConfig.getPort(),
                redisConfig.getTimeout() * 1000, redisConfig.getPassword(), 0);
        return jp;
    }

}
